package com.somlsis.fragment

import android.content.Intent
import com.base.AppConfig
import com.base.BaseFragment
import com.net.HttpResponse
import com.somlsis.activity.AboutUsActivity
import com.somlsis.activity.AccountManagementActivity
import com.somlsis.activity.AuthorityManagementActivity
import com.somlsis.activity.DataStatisticsActivity
import com.somlsis.activity.FeedbackActivity
import com.somlsis.activity.UserInfoActivity
import com.somlsis.bean.UserOverview
import com.somlsis.databinding.FragmentMineBinding
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class MineFragment : BaseFragment<FragmentMineBinding>() {
    private var userOverview: UserOverview? = null
    override fun getViewBinding(): FragmentMineBinding {
        return FragmentMineBinding.inflate(layoutInflater)
    }

    override fun createView() {
        dataCenter.getUserOverview(object : Callback<HttpResponse<UserOverview>> {

            override fun onResponse(
                call: Call<HttpResponse<UserOverview>>,
                response: Response<HttpResponse<UserOverview>>
            ) {
                if (response.code() == AppConfig.SUCCEED_CODE) {
                    response.body()?.let {
                        userOverview = it.data
                        refreshUI()
                    }
                }
            }

            override fun onFailure(call: Call<HttpResponse<UserOverview>>, t: Throwable) {

            }

        })
        binding.personalInfo.setOnClickListener{
            startActivity(Intent(activity,UserInfoActivity::class.java))
        }
        binding.accountManagement.setOnClickListener{
            startActivity(Intent(activity,AccountManagementActivity::class.java))
        }
        binding.motionStatistics.setOnClickListener{
            startActivity(Intent(activity,DataStatisticsActivity::class.java))
        }
        binding.authorityManagement.setOnClickListener{
            startActivity(Intent(activity,AuthorityManagementActivity::class.java))
        }
        binding.aboutUs.setOnClickListener{
            startActivity(Intent(activity,AboutUsActivity::class.java))
        }
        binding.questionsAndComments.setOnClickListener{
            startActivity(Intent(activity,FeedbackActivity::class.java))
        }
    }

    override fun refreshUI() {
        userOverview?.let {
            binding.cumulativeRun.text = it.mileage
            binding.totalTime.text = it.time
            binding.numberMovements.text = it.nums

            binding.farthestDistance.text = it.person_mileage
            binding.maximumTime.text = it.person_time
            binding.maximumPace.text = it.person_pace
        }

    }

}